﻿//TODO: test,  Moravec

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Corner detection filter
    /// </summary>
    public class Moravec : BaseForeBackgroundColorFilter, ICornerFinder
    {
        protected int m_nThreshold = 10;
        protected int m_nWindow = 3;
        protected List<Point> m_aPoint = new List<Point>();

        /// <summary>
        /// 
        /// </summary>
        public Moravec()
            : this(10, 3)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_nThreshold"></param>
        /// <param name="_nWindow"></param>
        public Moravec(int _nThreshold, int _nWindow)
        {
            m_nThreshold = _nThreshold;
            m_nWindow = _nWindow;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_moravec"></param>
        internal Moravec(Moravec _moravec)
        {
            this.m_nThreshold = _moravec.m_nThreshold;
            this.m_nWindow = _moravec.m_nWindow;
        }

        /// <summary>
        /// Gets the points
        /// </summary>
        public List<Point> Points
        {
            get { return m_aPoint; }
        }

        /// <summary>
        /// Gets or sets threshold
        /// </summary>
        public int Threshold
        {
            get { return m_nThreshold; }
            set { m_nThreshold = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public int Window
        {
            get { return m_nWindow; }
            set { m_nWindow = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            //First implementation
            UnsafeBitmap bitmap = _bitmap;

            List<Point> aPoint = Definitions.EnvironmentPointList;
            int[,,] aSum = new int[bitmap.Width, bitmap.Height,8];
            Color[] aColor = new Color[8];

            Graphics graphics = Graphics.FromImage(_bitmap.InternalBitmap);

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            Color color = Color.Transparent;

            int y = 0;
            int x = 0;

            for (y = 1; y < nHeight - 1; y++)
            {
                for (x = 1; x < nWidth - 1; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    for (int i = 0; i < 8; i++)
                    {
                        aColor[i] = bitmap.GetPixel(x + aPoint[i].X, y + aPoint[i].Y);
                        aSum[x,y,i] += (int)ColorFunctions.Distance2(color, aColor[i]);
                    }
                }
            }

            for (y = 1; y < nHeight - 1; y++)
            {
                for (x = 1; x < nWidth - 1; x++)
                {
                    bitmap.SetPixel(x, y, Min(x, y, aSum) > 1 ? m_ColorForeground : m_ColorBackground);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="aSum"></param>
        /// <returns></returns>
        protected int Min(int x, int y, int[,,] aSum)
        {
            int[] aInt = new int[8];
            for (int i = 0; i < 8;i++ )
            {
                aInt[i] = aSum[x,y,i];
            }
            Array.Sort(aInt);
            return aInt[0];
        }

        /// <summary>
        /// Clones this instance
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Moravec(this);
        }
    }
}
